﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Web.SessionState;
using SZHL.UI.AppCode;

namespace SZHL.UI.API
{
    /// <summary>
    /// FTAPI 的摘要说明
    /// </summary>
    public class SZHLAPI : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            string strAction = context.Request["Action"] ?? "";
            string P1 = context.Request["P1"] ?? "";
            string P2 = context.Request["P2"] ?? "";

            Msg_Result Model = new Msg_Result() { Action = strAction.ToUpper(), ErrorMsg = "" };
            try
            {
                if (strAction.ToUpper() == "LOGIN".ToUpper())
                {
                    string strUserName = context.Request["UserName"] ?? "";
                    string strPassWord = context.Request["password"] ?? "";

                    JH_Auth_UserB BUser = new JH_Auth_UserB();
                    int count = BUser.GetEntities(d => d.UserName == strUserName && d.UserPass == strPassWord && d.IsUse == "Y").Count();
                    if (count == 0)
                    {
                        Model.ErrorMsg = "用户名或密码错误";
                    }
                    else
                    {
                        //获取用户名
                        //根据用户名更新Code和时间
                        string strGuid = System.Guid.NewGuid().ToString();
                        var jau = new SZHL.UI.AppCode.JH_Auth_UserB().GetEntity(p => p.UserName == strUserName);
                        if (jau != null)
                        {
                            jau.pccode = strGuid;
                            jau.pccodeupdate = DateTime.Now;
                            if (new SZHL.UI.AppCode.JH_Auth_UserB().Update(jau))
                            {
                                Model.ErrorMsg = "";
                                Model.Result = strGuid;
                            }
                        }
                    }
                
                }
                else
                {
                    #region 必须登录执行接口
                    Model.ErrorMsg = "";
                    string strUserName = string.Empty;
                    if (context.Request.Cookies["szhlcode"] != null)
                    {
                        //通过Cookies获取Code
                        string szhlcode = context.Request.Cookies["szhlcode"].Value.ToString();
                        //通过Code获取用户名，然后执行接口方法
                        var jau = new SZHL.UI.AppCode.JH_Auth_UserB().GetEntity(p => p.pccode == szhlcode);
                        if (jau == null)
                        {
                            jau = new SZHL.UI.AppCode.JH_Auth_UserB().GetEntity(p => p.mobcode == szhlcode);
                        }
                        if (jau != null)
                        {
                            var container = SZHLServiceContainer.Current().Resolve<IWsService>(strAction.ToUpper());
                            JH_Auth_UserB.UserInfo UserInfo = new JH_Auth_UserB.UserInfo(jau.UserName);
                            container.ProcessRequest(context, ref Model, P1, P2, UserInfo);
                        }
                        else
                        {
                            Model.ErrorMsg = "NOSESSIONCODE";
                        }
                    }
                    else {
                        Model.ErrorMsg = "NOSESSIONCODE";
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                //Model.ErrorMsg = "操作异常，请重试";
                Model.ErrorMsg = ex.Message;
            }
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string Result = JsonConvert.SerializeObject(Model, Formatting.Indented, timeConverter).Replace("null", "\"\"");
            context.Response.Write(Result);


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}